 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author indu.chawla
 */
import com.aliasi.matrix.SvdMatrix;
import com.aliasi.tokenizer.PorterStemmerTokenizerFactory;
import java.io.*; 
import java.util.Arrays;
import java.sql.*;
import java.util.*;

public class Indu_LSIModel {
    private double[][]  array2;
    private double[][] transpose;
    private double[][]  scale2;
    private double[][] LSITERM_TERM_MATRIX;
    private double[][] TERM_DOCUMENT_MATRIX;
    private int NUM_FACTORS = 2;
    private String[] TERMS;
    private String[] DOCS;
    
    ArrayList<Double> scorearr = new ArrayList<Double>();
    ArrayList<Integer> index = new ArrayList<Integer>();
    
    ArrayList<String> Issues = new ArrayList<String>();
    ArrayList<String> Component = new ArrayList<String>();
    
    public void setTerms(String[] terms){
        //System.arraycopy(TERMS,0,terms,0,(terms.length()-1));
    	this.TERMS = terms;
    }

    public void setDocs(String[] docs){
    	this.DOCS = docs;
    }
    
    public void setTermDocumentMatrix(double[][] tdm){
    	this.TERM_DOCUMENT_MATRIX = tdm;
    }
    
    public void setIssues(ArrayList<String> Issues) {
        this.Issues = Issues;
    }
    
    public void setComponents(ArrayList<String> Component) {
        this.Component = Component;
    }
    
    public void process(String[] ags) {
    	
    try{
    	double featureInit = 0.1;
        double initialLearningRate = 0.005;
        int annealingRate = 10;
        double regularization = 0.00;
        double minImprovement = 0.0001;
        int minEpochs = 10;
        int maxEpochs = 50;

        System.out.println("  Computing SVD");
        System.out.println("    maxFactors=" + NUM_FACTORS);
        System.out.println("    featureInit=" + featureInit);
        System.out.println("    initialLearningRate=" + initialLearningRate);
        System.out.println("    annealingRate=" + annealingRate);
        System.out.println("    regularization" + regularization);
        System.out.println("    minImprovement=" + minImprovement);
        System.out.println("    minEpochs=" + minEpochs);
        System.out.println("    maxEpochs=" + maxEpochs);

        SvdMatrix matrix
            = SvdMatrix.svd(TERM_DOCUMENT_MATRIX,
			    NUM_FACTORS,
			    featureInit,
			    initialLearningRate,
			    annealingRate,
			    regularization,
                            null,
			    minImprovement,
			    minEpochs,
			    maxEpochs);

	double[] scales = matrix.singularValues();
	double[][] termVectors = matrix.leftSingularVectors();
	double[][] docVectors = matrix.rightSingularVectors();
        
         
	System.out.println("\nSCALES");
	for (int k = 0; k < NUM_FACTORS; ++k)
	    System.out.printf("%d  %4.2f\n",k,scales[k]);
	System.out.print("THESE ARE TERMS");
for (int i = 0; i < termVectors.length; ++i) {
    
    System.out.println(TERMS[i]);
}
	
	System.out.println("\nTERM VECTORS");
	for (int i = 0; i < termVectors.length; ++i) {
	    System.out.print("(");
	    for (int k = 0; k < NUM_FACTORS; ++k) {
		if (k > 0) System.out.print(", ");
		System.out.printf("% 5.2f",termVectors[i][k]);
	    }
	    System.out.print(")  ");
	    System.out.println(TERMS[i]);
	}
	 for(int t = 0; t<10 ;++t){
                System.out.print(DOCS[t]);
            }
	System.out.println("\nDOC VECTORS");
	for (int j = 0; j < docVectors.length; ++j) {
             System.out.print("(");
	    for (int k = 0; k < NUM_FACTORS; ++k) {
		if (k > 0) System.out.print(", ");
		System.out.printf("% 5.2f",docVectors[j][k]);
                  
	    }
	    System.out.print(")  ");
	    System.out.println(DOCS[j]);
            
	}
	 //args[]= {"This counter ", "is", "an","elements", "could", "potential", "rewrap", "and", "cause", "an", "ever", "growing", "processor", "path"} ;
      /*  BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("2 do you wish to enter?");
            String input = stdin.readLine();
            int nameCount = Integer.parseInt(input);
            String[] ags = new String[nameCount];
            for(int i = 0; i<nameCount; i++) {
                System.out.print("Enter query: ");
                //NEED HELP HERE
                ags[i] = stdin.readLine();*/
        
      /*  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Please enter query string:");
                        String ags[] = bf.readLine(); */
        
      //  String ags[] = {"crash enable user script"};
        
        
        
        
        
        
        
        
        
	for (int m = 0; m < ags.length; ++m) {

            
            preprocess pf = new preprocess();
                        
                        ags[m]=pf.clean(ags[m]);
                         ags[m]=pf.remove_stop_words(ags[m]);
                          ags[m]=pf.apply_stemming(ags[m]);                          
                                 
            System.out.println(ags[m]);
            
            
	    search(scales,termVectors,docVectors,ags[m]);
	}

    }catch(Exception e){
    	System.out.println(e);
    }
    	
   }


    public void search(double[] scales,
		       double[][] termVectors,
		       double[][] docVectors,
		       String arg) {
	String[] terms = arg.split(" |,"); // space or comma separated
         

/*	for(int i=0;i<terms.length;i++){
		String token = PorterStemmerTokenizerFactory.stem(terms[i].trim().toLowerCase());
		terms[i] = token;
	}*/
	
	
	double[] queryVector = new double[NUM_FACTORS];
	Arrays.fill(queryVector,0.0);

	for (String term : terms)
	    addTermVector(term,termVectors,queryVector);

	
	System.out.println("\nQuery=" + Arrays.asList(terms));
	System.out.print("Query Vector=(");
	for (int k = 0; k < queryVector.length; ++k) {
	    if (k > 0) System.out.print(", ");
	    System.out.printf("% 5.2f",queryVector[k]);
	}
	System.out.println(" )");

	/*System.out.println("\nDOCUMENT SCORES VS. QUERY");
	for (int j = 0; j < docVectors.length; ++j) {
	    double score = dotProduct(queryVector,docVectors[j],scales);
	    //double score = cosine(queryVector,docVectors[j],scales);
	    System.out.printf("  %d: % 5.2f  %s\n",j,score,DOCS[j]);
	    //System.out.printf("  %d: % 5.2f  %s\n",j,score,j);
	}

	System.out.println("\nTERM SCORES VS. QUERY");
	for (int i = 0; i < termVectors.length; ++i) {
	    double score = dotProduct(queryVector,termVectors[i],scales);
	    // double score = cosine(queryVector,termVectors[i],scales);
	    System.out.printf("  %d: % 5.2f  %s\n",i,score,TERMS[i]);*/
        System.out.println("\nDOCUMENT SCORES VS. QUERY");
	for (int j = 0; j < docVectors.length; ++j) {
	    double score = dotProduct(queryVector,docVectors[j],scales);
	    // double score = cosine(queryVector,docVectors[j],scales);
	    System.out.printf("  %d: % 8.7f  %s\n",j,score,DOCS[j]);
            scorearr.add(score);
            index.add(j);
	}

        bubblesort(scorearr,Issues,Component);
        int index1 = scorearr.size();

                        // ENTER THE NUMBER OF Issues to be displayed in result
                        int num = 10;
                        while(index1 > (scorearr.size()-num))
                        {
                            System.out.print(" Score is  " + scorearr.get(index1-1).floatValue());
                            System.out.print("   Issue id is  " + Issues.get(index1-1));
                            System.out.println("  Component is  " + Component.get(index1-1));
                            index1--;
                        }
    
        //To Print TERM SCORES VS. QUERY
        
	/* System.out.println("\nTERM SCORES VS. QUERY");
	for (int i = 0; i < termVectors.length; ++i) {
	    double score = dotProduct(queryVector,termVectors[i],scales);
	    // double score = cosine(queryVector,termVectors[i],scales);
	    System.out.printf("  %d: % 8.7f  %s\n",i,score,TERMS[i]);
	}	*/
    }


    /*public void addTermVector(String term, double[][] termVectors, double[] queryVector) {
	for (int i = 1; i < TERMS.length; ++i) {
            //System.out.println("TERMS.length"+TERMS.length);
            //System.out.println("TERMS"+TERMS[i]);
	    if (TERMS[i].equals(term)) {
		for (int j = 0; j < NUM_FACTORS; ++j) {
		    queryVector[j] += termVectors[i][j];
		}}
            else{
                System.out.println("TERMS"+TERMS[i]);
                System.out.println("terms"+term);}
                        
		return;
	    
	}
    }*/
    public void addTermVector(String term, double[][] termVectors, double[] queryVector) {
	for (int i = 0; i < TERMS.length; ++i) {
	    if (TERMS[i].equals(term)) {
		for (int j = 0; j < NUM_FACTORS; ++j) {
		    queryVector[j] += termVectors[i][j];
		}
		return;
	    }
	}
    }
    
public double dotProduct(double[] xs, double[] ys, double[] scales) {
	double sum = 0.0;
	for (int k = 0; k < xs.length; ++k)
	    sum += xs[k] * ys[k] * scales[k];
       // System.out.println("sum" +sum);
	return sum;
    }
   /* public double dotProduct(double[] xs, double[] ys, double[] scales) {
	double sum = 0.0;
	for (int k = 0; k < xs.length; ++k)
	    sum += xs[k] * ys[k] * scales[k];
	return sum;
    }*/

    public double cosine(double[] xs, double[] ys, double[] scales) {
	double product = 0.0;
	double xsLengthSquared = 0.0;
	double ysLengthSquared = 0.0;
	for (int k = 0; k < xs.length; ++k) {
	    double sqrtScale = Math.sqrt(scales[k]);
	    double scaledXs = sqrtScale * xs[k];
	    double scaledYs = sqrtScale * ys[k];
	    xsLengthSquared += scaledXs * scaledXs;
	    ysLengthSquared += scaledYs * scaledYs;
	    product += scaledXs * scaledYs;
	}
	return product / Math.sqrt(xsLengthSquared * ysLengthSquared);
    }
    
    public static void bubblesort(ArrayList<Double> arr1,ArrayList<String> arr2,ArrayList<String> arr3)
    {
        int i, j,n=0;
        n = arr1.size();
        double t=0;
        //int t1;
        String t1,t2;
        for(i = 0; i < n; i++)
        {
            for(j = 1; j < (n-i); j++)
            {
                if(arr1.get(j-1) > arr1.get(j))
                {
                    t = arr1.get(j-1);
                    arr1.set(j-1, arr1.get(j));
                    arr1.set(j, t);

                    t1 = arr2.get(j-1);
                    arr2.set(j-1, arr2.get(j));
                    arr2.set(j, t1);
                    
                    t2 = arr3.get(j-1);
                    arr3.set(j-1, arr3.get(j));
                    arr3.set(j, t2);
                    
                     
                }
            }
        }
   }
    
public static void main(String[] args) {
     
        ArrayList<String> Issueidarr = new ArrayList<String>();
        ArrayList<String> Comparr = new ArrayList<String>();
        
                try {           
                        String dbUser = "root";
                        String dbPswd = "indujiit";
                        String dbUrl = "jdbc:mysql://localhost/mysql";
                        Connection conn;
                       
                                                        
                        
                        
                        Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
                        conn = DriverManager.getConnection( dbUrl, dbUser, dbPswd );                        
                                                                    
                         
 
                        Statement s0 = conn.createStatement();
			s0.executeQuery( "SELECT * FROM chrome.chrometen");
			ResultSet rs2 = s0.getResultSet();
                        TermDocumentMatrix tdm = new TermDocumentMatrix();
                        String link=null,link2=null,link3=null,link4=null,link5=null;
                        tdm.setNumDocuments(10);
                        int index=0;
                        Vector <String> terms =new Vector <String>();
                        Vector <String> docs =new Vector <String>();
                        while(rs2.next())
                        {
                       /* mydata.Bugid=rs2.getString(1);
                        mydata.title=rs2.getString(2);
                       mydata.description =rs2.getString(3);*/
                       
                       String issueid=rs2.getString(1);
                       String title= rs2.getString(2);
                       String desc = rs2.getString(3);
                       String component = rs2.getString(4);
                       String Comment = rs2.getString(5);
                       
                        Issueidarr.add(issueid);
                        Comparr.add(component);
                       
                      /* mydata.issueid=rs2.getString(1);
                        mydata.title=rs2.getString(7);
                       mydata.description =rs2.getString(8);
                       
                       System.out.println("mydata.issueid"+ mydata.issueid);
                       System.out.println("mydata.title"+ mydata.title);
                       System.out.println("mydata.description"+ mydata.description);*/
                                               
                        
                       // Vector<String> terms =new Vector< String>();
                        preprocess pf = new preprocess();
                        
                        title=pf.clean(title);
                           desc=pf.clean(desc);
                            Comment=pf.clean(Comment);
                            title = pf.remove_SW(title);
                            desc=pf.remove_SW(desc);
                            Comment=pf. remove_SW(Comment);
                            
                                title=pf.remove_stop_words(title);
                                desc=pf.remove_stop_words(desc);
                                Comment=pf.remove_stop_words(Comment);
                            title=pf.apply_stemming(title);
                            desc=pf.apply_stemming(desc);
                             Comment=pf.apply_stemming(Comment);
                          
                             
                             /*System.out.println("----------------------");
                                System.out.println("clean Title=" +title);
                                System.out.println("Clean desc=" +desc);
                                System.out.println("Clean comments=" +Comment);*/
                                //link = title + desc + Comment;
                                 link=title + desc;   
                                 System.out.println("link" +link +index);
                                System.out.println("========================");                                  
                                              
                         
                        if(! (terms.isEmpty())) 
                        terms.clear();
                        //docs.addElement(link);
                       //link5=link5+link;
                       /* Enumeration VVnum=terms.elements();
                System.out.println("terms in vector");
                while( VVnum.hasMoreElements())
                        System.out.print(VVnum.nextElement()+" ");
                        System.out.println();*/
                        
                        //   int index=1;
                       //terms = mytest.getUniqueTerms();
                        
                       // mytest.setNumDocuments(48);
                      
      String delims = "[ ]+";
 
String[] tokens = link.split(delims);
for (int i = 0; i < tokens.length; i++)
    terms.addElement(tokens[i]);
     
                        tdm.update(link,terms,index);
                        index = index +1;
                       // System.out.println("terms in vector");
                        // double[][] T;
                        //System.out.print(terms);
                     //  T = mytest.getTermDocumentMatrix();
                        //mytest.processTermDocumentMatrix();
                         
                        //mytest.update(link,terms,index);
                       // mytest.getTermDocumentMatrix();
                        
                       /*  System.out.println( mytest.Bugid);
                         System.out.println( link);
                        System.out.println(mytest.description );*/
                        
                        }
                        Vector <String> doc= tdm.getDocuments();
                        Enumeration VEnum=doc.elements();
                System.out.println("elements in vector");
                while( VEnum.hasMoreElements())
                        System.out.print(VEnum.nextElement()+" ");
                        System.out.println();
                        System.out.println("index" +index);
                        System.out.println();
                        
                        
                        Vector <String> just = tdm.getUniqueTerms();
                       Enumeration VVnum=just.elements();
              /*  System.out.println("uniqueterms ");
                while( VVnum.hasMoreElements())
                { System.out.print(VVnum.nextElement()+" ");
                        System.out.println();}*/
                
                        tdm.processTermDocumentMatrix();
                       
                       
                       String[] criteria = new String[just.size()];
                        just.toArray(criteria);
                        //criteria = criteria.split(" |,");
                        Indu_LSIModel lsi= new  Indu_LSIModel();
                        lsi.setIssues(Issueidarr);
                        lsi.setComponents(Comparr);
                       lsi.setTerms(criteria);
                       String[] documents = new String[doc.size()];
                        doc.toArray(documents);
                        
                       lsi.setDocs(documents);
                       lsi.setTermDocumentMatrix( tdm.getTermDocumentMatrix());
                    /*   String[] query = new String [10];
                        System.out.println("enter the query");
                        for(int i = 0; i<10; i++) { 
                          //  System.out.print("Enter name: ");
                              query[i] = stdin.readLine();
                }*/
                       String a[] = {"usr","net"};
                                              lsi.process(a);
                       
                
                }       

                 

                        
                

                catch (Exception ex) 
                {
                	 ex.printStackTrace();		
                }  
    }
 }


 
